package com.han.service.user;

import com.han.dao.BaseDao;
import com.han.dao.user.UserDao;
import com.han.dao.user.UserDaoImpl;
import com.han.pojo.Role;
import com.han.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService{
    //业务层都会调用dao层.所以我们要引入Dao层（重点）
    //只处理对应业务

    //定义无参的构造方法创建usedao，在创建本方法是自动创建
    private UserDao userDao;
    public UserServiceImpl(){
        userDao = new UserDaoImpl();
    }
    public User login(String userCode) {
        Connection connection=null;
        User user = null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getUserLogin(connection,userCode);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection,null,null);
        }
        return user;
    }

    public int updateUser(int id, String userPassword) {
        Connection connection = null;
        int i = 0;
        try {
            connection = BaseDao.getConnection();
            i = userDao.updateUser(connection, id, userPassword);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection, null, null);
        }
        return i;
    }

    public int getUserCount(String userName, int userRole) {
        Connection connection = null;
        int count = 0;
        try {
            connection = BaseDao.getConnection();
            UserDao userDao = new UserDaoImpl();
            count = userDao.getUserCount(connection, userName, userRole);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection,null,null);
        }
        return count;
    }

    public List<User> getUserList(String userName, int userRole, int currentPageNo, int pageSize) {
        Connection connection =null;
        List<User> userList = new ArrayList<User>();
        try {
            connection = BaseDao.getConnection();
            userList = userDao.getUserList(connection, userName, userRole, currentPageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return userList;
    }

    public int addUser(List<Object> user) {
        Connection connection = null;
        int i = 0;
        try {
            connection = BaseDao.getConnection();
            i = userDao.addUser(connection, user);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return i;
    }

    public boolean deleteUser(int id) {
        Connection connection = null;
        boolean is = false;
        try {
            connection = BaseDao.getConnection();
            is = userDao.deleteUser(connection, id);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection, null, null);
        }
        return is;
    }

    public User getUserById(int id) {
        Connection connection = null;
        User user = null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getUserById(connection, id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }

    public int ModifyUser(User user) {
        Connection connection = null;
        int i = 0;
        try {
            connection = BaseDao.getConnection();
            i = userDao.ModifyUser(connection, user);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return i;
    }
}
